Okay.
In this lecture, let's go ahead and give our players the ability to purchase guns and other items from
our shop.
This means we'll have to listen for when the proximity prompt inside of our shop is triggered, and
then display a shop GUI on the player's screen.
Then the player can press a button to purchase an item, fire to the server that they want to buy something,
and then the server can check if the player has enough money, and then give them the tool and deduct
the money out of their leaderboard.
So the GUI we're going to be displaying on the player's screen is inside of replicated storage in the
shared folder.
And then in the GUI folder.
It's our shop GUI, and I'll parent it into the starter GUI so we can go ahead and take a look at it.
Let me enable UI visibility.
This is our simple shop GUI, and it's simply just a scrolling frame that has different frames inside
of it for the different items we can purchase.
And each of these items has a button inside of it with a name for the item they would like to purchase.
So if they hit this button, then we're going to tell a server we would like to purchase a grenade.
Or for example, if they hit this button, we'll tell the server we want to purchase the pistol and
so on.
So the server's job is going to listen for when the player clicks these buttons.
And then we're going to check to see if the player has enough money.
And if they do we can give them the item.
So let me go ahead and move this back inside of the GUI folder.
And then let me disable UI visibility.
And let's go ahead and create a new script inside of server script service.
And this can be the script responsible for handling our shop.
So we'll call it the shop handler.
We'll be using the same comet template as before.
And what we want to do is we want to enable this proximity prompt that is in our shop only when the
game is set, either in the starting intermission or the intermission between waves.
So when a wave starts or the game is currently in cleanup mode, we want to basically close down the
shop.
We want to hide this guy up here and disable the proximity prompt so players can't purchase stuff while
a wave is going on.
So what we can go ahead and do is we can listen for when the workspace has an attribute changed.
And that attribute we're going to get attribute change signal.
And that is going to be the game state attribute.
And then we can connect a function to this event.
And then we can go ahead and get the current game state which is equal to workspace get attribute game
state.
And then we can check to see what the current game state is, and if it's in the intermission or start
intermission, we'll enable the shop.
Otherwise we'll disable the shop.
So let's go ahead and get the game state enum and Replicated Storage.
So we'll grab replicated storage.
And then we can go ahead and get our game state enum and we'll require from replicated storage dot modules,
dot enums dot game state enum.
So if the workspace is current, game state is equal to the game state enum of intermission, or the
game state is equal to the game state enum of start intermission, then we're okay to go ahead and enable
that proximity prompt and the billboard guy over our shop.
So let's go ahead and make a variable for those things.
We first want to refer to our shop.
And that's in the workspace in the map folder.
And it's simply called our I believe Weapon shop.
There we go.
We can go ahead and grab the prompt inside of our weapon shop, which is inside of our shop.
We're going to get the proximity part and then get the prompt that's inside of there.
And then we can also grab the billboard guy.
We'll just call it guy and that's equal to shop and we'll grab our billboard guy.
So in the game state is in the intermission.
Then we're going to make sure that our prompt is enabled.
So we'll set that equal to true.
And we'll also make sure our guy is enabled.
Otherwise if the game state is not in the intermission, then we'll make sure that our prompt is disabled
and that our guy is also disabled.
Then from this point, we could go ahead and listen to when our prompt is triggered by a player.
So we'll connect a function and get that player.
And what we want to go ahead and do is we want to clone the guy that is in replicated storage and give
it to our player.
So the first thing we need to do is just in case.
For some reason, the game state is not in the intermission game state.
We can check inside of here as well.
So I'm going to copy this and paste this here.
So if the game state is not equal to the regular intermission and the game state is not equal to the
start intermission, then we don't want to give them a GUI.
So we're just going to return.
Otherwise we got to check if the player already has this guy displayed on their screen.
So if this player and in their player GUI folder we find first child which is the shop GUI, then that
means we shouldn't give the player a new guy so we could either return, or another thing we could do
is we could refer to that player's GUI and then just destroy it so it's not on their screen anymore.
So shop GUI destroy.
And then we could return, or we could just actually give and display them a new one.
But in this case, we're just going to create a new clone of our shop guy.
So let's go ahead and make a variable for that.
We'll call it our shop guy.
And that's in replicated storage dot shared dot GIS dot shop guy.
And then we can go ahead and clone our shop guy and then set the parent of that clone equal to our players
player guy folder.
So it displays on their screen.
And then this is where we would have a local script inside of that shop guy.
And then once we parent it to the players player guy folder, that local script will execute on the
client side, allowing the player to interact with the buttons in the GUI.
So actually let's go ahead and go into replicated storage and let's create a new local script inside
of our shop GUI.
And this can be our shop GUI handler.
We'll use the same common template.
And inside of here we can go ahead and make a variable to refer to the GUI itself, which is script
dot parent.
And then we want to go ahead and refer to the frame holding all of the stuff in the GUI.
So that's equal to g dot background frame.
And then we want to go ahead and also grab the close button of our shop.
So if the player presses the close button we'll remove it from the player.
So that'll be equal to frame dot close button.
And then we'll also get the frame that's holding all of the different items in our shop.
So that's the shop frame and that's going to be frame dot shop frame.
Now in order to communicate to the server when we want to buy something, we have an event inside of
the remotes folder inside of GUI, which is our buy item event, so we can go ahead and grab replicated
storage.
And then we'll make a reference to that event.
We'll just call it our BI event that's equal to replicated storage, dot remotes, dot gui dot buy item.
Then what we can go ahead and do is to make this really simple.
Instead of having to refer to every single button that is inside of our shop frame, we can instead
loop through all of the descendants of our shop frame, check to see if it's one of these frames, and
then grab the button in there, and then listen for when that button is clicked and fire to the server
the name of this button so the server knows which item the player wants to purchase.
So we could create a dedicated function to listen for when a particular button is clicked.
We'll call it on button clicked, and we'll get passed to this function.
The name of the item they want to purchase, which should be the name of the button.
And then we could use our buy event and fire to the server.
Hey, we want to buy this particular item, so then we can loop through all of the buttons inside of
our shop frame get.
Descendants.
And what we want to do is we want to make sure that this element that we're looping through is actually
a button.
So if this particular element is not a button, so is a text button.
If it is not a text button, then we're just going to continue to the next iteration of this loop.
Otherwise we can go ahead and listen for when this button is activated.
And then we'll connect a function.
And this is where we would go ahead and call our on button clicked, and then pass the name of our button.
And then the other thing we want to go ahead and listen to is when our close button is activated.
And then we can connect a function and simply just disable the GUI.
And since we're doing it this way, that means the GUI is still going to be inside of our player GUI
folder.
And that means the next time a player goes and they trigger the proximity prompt to reopen it on their
screen, the server is simply going to delete their previous one and give them a new one.
And I believe that's all we need to do for our shop guy.
So back inside of our shop handler, we need to go ahead and listen to that by event and check to see
if the player has enough money to purchase a particular item.
So let's go ahead and make a variable for our by event.
And that's in replicated storage dot remotes dot guy dot buy item.
And then we can go ahead and listen to when our by event is fired.
So on server event will connect a function.
We'll get the player and then the name of the item they wish to purchase.
And then what we could go out and do is create a table containing the names of all of the different
items that are possible to purchase.
So I'll call this my tools table.
And inside of here we'll have a key which represents the name of a particular item.
And then we could set the price and then the tool for that particular item.
So for example if one of the buttons the player presses is the pistol button, then the client is going
to send to the server the name of pistol.
So we'll make that one of the keys in our table.
So pistol.
And we'll set that equal to a table.
And inside of here we can define the price of that particular item as well as the tool associated with
this name.
So for the pistol inside of our GUI we set that to the price of $60.
And then the associated tool with that is stored in server storage.
So let's go ahead and make a reference to server storage game git service server storage.
And the tool is in server storage tools.
And we'll get the pistol.
Another item that the player can purchase is the SMG.
And the button is called SMG.
And we'll set that equal to another table.
The price for that is set at 100.
And then the associated tool is equal to the server storage dot tools dot SMG.
And then we can just continue doing this for every single purchasable item in our game.
For example they can purchase a rifle and the rifle costs 200.
And then we'll refer to our rifle, which I believe it's called assault rifle.
There we go.
Another item we can purchase is a regular shotgun.
The price we set for that one is also set at 200.
And that's going to be equal to our shotgun.
And we'll do the buckshot shotgun.
The next item they can purchase would be the shotgun.
And the button is called shotgun slugs.
And we set the price of that one equal to 250.
And that's going to be our shotgun of slugs.
The next one would be the semi-auto shotgun I believe that's how we named it.
So if we go ahead and take a look at the button for that one.
Yep.
It says semi-auto shotgun.
So we'll make sure that this key matches the name of the button.
And then we'll refer to that inside of our tools folder that is the semi-auto shotgun.
And then players could also purchase other things such as a mic kit.
So we'll call this one med kit.
The price of a med kit we set to $30 and the tool is our med kit.
And then last but not least, in the future we're going to be scripting our grenade.
But just for demonstration sake, we're going to also add our grenade into our table.
Forgot a comma there.
And we put that there.
And the price for our grenade is equal to $40.
And the tool is going to be in service storage dot tools dot grenade.
So now what we can go ahead and do is when this event is fired, we first want to make sure that the
game is in a state where the player can purchase an item because an exploiter could fire this event
while the players are in the middle of a wave.
And we don't want to give that player an item unless the game is in the intermission state.
So we're basically just going to copy this here and paste that there.
So if it's not in intermission, we're going to return.
Otherwise we also need to verify that the name of the item they passed here is actually a possible item
to purchase within our tools table.
So if we do not find this particular tool or this name of the item in our tools table, then we're also
just going to return.
Another thing we need to check is if the player has enough money to purchase this particular tool.
So we're going to calculate what the after balance would be for our player.
When we subtract the price of this tool to the current amount of money the player has.
So that's going to be equal to player.
Get their leaderstats folder, get the money, and then get the value of that and subtract it by this
particular tools.
And then we'll pass the name of the item and we'll get the price of that tool.
And then we can check if this after balance is less than zero.
So they would have a negative balance.
And that clearly means the player did not have enough money to purchase this item.
So we're also going to return.
Another thing we need to check for is if this player already has this tool in their inventory, because
we don't want players to purchase a duplicate guns, the only things that we would allow them to purchase
as duplicates would be the one use Medkits.
So if inside of our player's backpack we find first child.
And then we need to get the name of the tool.
So that's going to be in tools.
We'll pass the name of item, get that tool and then get the name of the tool.
If we find this tool in their backpack, or let's say we find it on their player's character.
So or player dot character find first child this.
And then we also need to make sure that the name of this item is in our med kit, because we want our
player to be able to purchase multiple med kits.
So then we'll put an end statement here.
So.
And let's check to see if the tools pass.
Name of item.
Get the tool and get the name.
If it's not equal to med kit, then we're going to return.
So let me go ahead and get rid of that duplicate then statement.
So.
This looks a little bit more concise.
Let me shift this down.
So if the player has this tool in their backpack or they have it equipped on their character, and the
name of this tool is not equal to our med kit, then we're going to ignore this player because we do
not want to give them duplicate tools.
Otherwise we can go ahead and update the value inside of the player's leader stats dot money int value
and set that value equal to our after balance.
And then we can go ahead and make a clone of the tool that they want to purchase.
So we'll clone that.
And then we can go ahead and set the parent of that clone equal to the player's backpack.
So now we should have scripted everything we need to allow players to purchase items from our shop.
So let's go ahead and make sure inside of our game handler script that we're setting the game state
to an intermission.
So we're setting it to the start intermission, which means we should be able to display all of the
items in our shop.
And actually, what we should do down here in this main section is we should check the game state of
the workspace.
So we'll get the game state if the game state is equal to.
The intermission or it's equal to the start intermission.
Then we'll make sure to enable the prompt and the GUI for the shop.
So we'll paste that right there.
So now we should be able to purchase items from our shop.
Let's go ahead and playtest and find out.
Go and play our game here.
And then we can go ahead and play our game and spawn into the map.
And let's go over and see if that prompt is enabled.
So as you can see it says open shop.
If I trigger the prompt, as you can see, the server cloned that guy into my player guy folder and
now it's displayed on my screen.
And since I don't have enough money to purchase anything, if I click these buttons, nothing should
happen.
So if I click.
As we expect nothing would happen because we don't have any money.
Now, if I go to the server and I give my player some money, so let me go ahead and give me a bunch
of money.
Do something like that.
Go back to the client.
Now I have $1 million.
Let's see if I can actually purchase something like a pistol.
So if I click this.
There we go.
I got the pistol added to my inventory, and then it deducted that amount of money from my money int
value.
I can also purchase a rifle.
Actually, it's not letting me purchase a rifle.
Very interesting.
How about an SMG?
I can purchase an SMG.
I can purchase a shotgun, a semi-auto shotgun.
I can purchase slugs, but it does look like I am not able to purchase a rifle, which could mean that
we got the name messed up a little bit.
So let's go ahead and confirm that the name of our button matches the server.
So the name of our button is rifle.
And then if we go back to our shop handler let's make sure that actually matches.
So yes, it's also spelt out as a rifle.
Right here we have the price set and then we should be referring to the assault rifle.
And it should be allowing us to purchase it.
I wonder why it didn't let us purchase the assault rifle.
That's why I already had the assault rifle inside of my starter pack.
So let me actually go ahead and delete it.
And then playtest my game.
I guess I didn't notice that I already had my assault rifle on me.
So let's go ahead and try that again.
If I open my shop.
As you can see, I can't purchase anything.
But if I give my player some money.
I'll just get my player a whole bunch of money and we'll go back.
And then if I were to purchase the rifle.
There we go.
I get the rifle, I get the pistol.
And then if I try to buy this item again, as you can see, it's not giving me anything because I already
have a rifle in my inventory, but I can buy all the other stuff as well as the grenade and the med
kit.
And then we shouldn't be limited on how many med kits we can purchase.
So if I keep clicking this.
As you can see, the server is continuing to give me med kits because we've set no limit on how many
med kits a player can buy.
Okay, we've got our shop all set up.
You're doing a great job so far with this project.
Keep up the good work and I'll see you in the next lecture.